<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科目名称" prop="subjectName">
        <el-input
            v-model="queryParams.subjectName"
            placeholder="请输入科目名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会计期间" prop="period">
        <el-input
            v-model="queryParams.period"
            placeholder="请输入会计期间"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['caiwu:general:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="generalList" :span-method="arraySpanMethod">
      <el-table-column label="科目编号" align="center" prop="subjectCode" />
      <el-table-column label="科目名称" align="center" prop="subjectName" />
      <el-table-column label="会计期间" align="center" prop="period" />
      <el-table-column label="摘要" align="center" prop="summary" />
      <el-table-column label="借方余额" align="center" prop="debitAmount" />
      <el-table-column label="贷方余额" align="center" prop="creditAmount" />
      <el-table-column label="方向" align="center" prop="direction" />
      <el-table-column label="余额" align="center" prop="balance" />
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="General">
import { listGeneral } from "@/api/caiwu/general";

const { proxy } = getCurrentInstance();

const generalList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const spanState = ref({}); // 用于存储合并信息

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    subjectName: null,
    period: null,
  }
});

const { queryParams } = toRefs(data);

// 计算合并单元格方法
const getSpanArr = (data) => {
  const spanStateObj = {
    subjectCode: [],
    subjectName: []
  };
  
  let pos = 0;
  let codeNamePeriod = '';
  
  data.forEach((item, index) => {
    if (index === 0) {
      spanStateObj.subjectCode.push(1);
      spanStateObj.subjectName.push(1);
      codeNamePeriod = item.subjectCode + item.subjectName + item.period;
      pos = 0;
    } else {
      const currentCodeNamePeriod = item.subjectCode + item.subjectName + item.period;
      if (currentCodeNamePeriod === codeNamePeriod) {
        spanStateObj.subjectCode[pos] += 1;
        spanStateObj.subjectName[pos] += 1;
        spanStateObj.subjectCode.push(0);
        spanStateObj.subjectName.push(0);
      } else {
        spanStateObj.subjectCode.push(1);
        spanStateObj.subjectName.push(1);
        codeNamePeriod = currentCodeNamePeriod;
        pos = index;
      }
    }
  });
  
  return spanStateObj;
};

// 合并单元格方法
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0 || columnIndex === 1) {
    const field = columnIndex === 0 ? 'subjectCode' : 'subjectName';
    const rowspan = spanState.value[field][rowIndex];
    const colspan = rowspan > 0 ? 1 : 0;
    return {
      rowspan,
      colspan
    };
  }
};

/** 查询总账列表 */
function getList() {
  loading.value = true;
  listGeneral(queryParams.value).then(response => {
    generalList.value = response.rows;
    total.value = response.total;
    // 计算合并信息
    spanState.value = getSpanArr(response.rows);
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('caiwu/general/export', {
    ...queryParams.value
  }, `general_${new Date().getTime()}.xlsx`)
}

getList();
</script>
